local lua__mingzhi = fk.CreateSkill{
  name = "lua__mingzhi",
  tags = { Skill.Permanent },
}

Fk:loadTranslationTable{
  ["lua__mingzhi"] = "明知",
  [":lua__mingzhi"] = "持恒技，<br>1.你不处于濒死状态时体力值不会不以此技能变化。"..
  "<br>2.你的体力值即将因一张牌而改变时，你将此牌置于你的武将牌上，称为“明知牌”。每轮开始时，你依次对自己使用明知牌，且每一张你未响应的明知牌结算结束后，你受到一点无来源伤害。"..
  "<br>3.当你因此技能而防止伤害时，你可以摸两张牌并弃置任意张牌。"..
  "<br>4.每个回合结束时，你可以对自己使用任意张明知牌，若如此做，你可以将本回合弃牌堆中的基本牌/锦囊牌/装备牌 当作 闪/无懈可击/杀使用或打出。"..
  "<br>5.若你的明知牌中有所有非装备牌名，你获得胜利。"..
  "<br>6.当你响应一张牌后，你可以移除一张相同牌名的明知牌。",
  ["@@lua__mingzhi-turn"] = "明知-闹事中",
  ["#lua__mingzhi-viewas"] = "明知：你可以将不因此法在本回合弃牌堆中的基本牌/锦囊牌/装备牌 当作 闪/无懈可击/杀使用或打出",
  ["#lua__mingzhi-defensive"] = "明知：是否摸两张牌并弃置任意张牌？",
  ["#lua__mingzhi-defensive-discard"] = "明知：你可以弃置任意张牌",
  ["#lua__mingzhi-asktocard"] = "明知：你可以选择任意张明知牌对自己使用",
  ["#lua__mingzhi-asktoremove"] = "明知：你可以移除一张与 %arg 相同牌名的明知牌",

  ["$lua__mingzhi1"] = "此事不过如此，于我，如清风拂面。",
  ["$lua__mingzhi2"] = "我早已了然，诸君，何须挂怀？",
  ["$lua__mingzhi3"] = "明知山有虎，绕虎便可行。",
  ["$lua__mingzhi4"] = "且备草船，向那曹操借箭十万！",
  --受伤
  ["$lua__mingzhi5"] = "此伤无碍，但实则难愈。",
  --吃桃
  ["$lua__mingzhi6"] = "此果鲜美，却是甜口毒药。",
}

lua__mingzhi:addEffect("viewas", {
  pattern = "slash,jink,nullification",
  prompt = "#lua__mingzhi-viewas",
  audio_index = {1,2,3,4},
  expand_pile = function(self, player)
    return player:getTableMark("lua__mingzhi-turn")
  end,
  handly_pile = false,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and table.contains(player:getTableMark("lua__mingzhi-turn"), to_select)
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 then return end
    local type = Fk:getCardById(cards[1]).type
    local card
    if type == Card.TypeEquip then
      card = Fk:cloneCard("slash")
    elseif type == Card.TypeBasic then
      card = Fk:cloneCard("jink")
    elseif type == Card.TypeTrick then
      card = Fk:cloneCard("nullification")
    else
      return nil
    end
    card:addSubcards(cards)
    card.skillName = lua__mingzhi.name
    return card
  end,
  enabled_at_play = function (self, player)
    return player:getMark("@@lua__mingzhi-turn") > 0 and #player:getTableMark("lua__mingzhi-turn") > 0
  end,
  enabled_at_response = function(self, player, response)
    return player:getMark("@@lua__mingzhi-turn") > 0 and #player:getTableMark("lua__mingzhi-turn") > 0
  end,
})

lua__mingzhi:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(lua__mingzhi.name, true) then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(player.room.discard_pile, info.cardId) then
              return true
            end
          end
        end
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.DiscardPile and not table.contains(player.room.discard_pile, info.cardId) then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local ids = player:getTableMark("lua__mingzhi-turn")
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(room.discard_pile, info.cardId) then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
      for _, info in ipairs(move.moveInfo) do
        if info.fromArea == Card.DiscardPile and not table.contains(room.discard_pile, info.cardId) then
          table.removeOne(ids, info.cardId)
        end
      end
    end
    room:setPlayerMark(player, "lua__mingzhi-turn", ids)
  end,
})
lua__mingzhi:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local ids = {}
  room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
    for _, move in ipairs(e.data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(room.discard_pile, info.cardId) then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
    end
  end, Player.HistoryTurn)
  room:setPlayerMark(player, "lua__mingzhi-turn", ids)
end)

lua__mingzhi:addEffect(fk.RoundStart, {
  anim_type = "negative",
  audio_index = {1,2,3,4},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(lua__mingzhi.name) and #player:getPile("lua__mingzhi") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = player:getPile("lua__mingzhi")
    for _, id in ipairs(cards) do
      local card = Fk:getCardById(id)
      if not card.is_passive then
        room:useCard({
          card = card,
          from = player,
          tos = { player },
          extraUse = true,
          extra_data = {
            lua__mingzhi_round = true,
          }
        })
      end
    end
  end,
})

lua__mingzhi:addEffect(fk.AfterAskForCardUse, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(lua__mingzhi.name) and data.eventData and data.eventData.use and data.eventData.use.extra_data and
    data.eventData.use.extra_data.lua__mingzhi_round and data.eventData.from ~= player and not (data.result and data.result.from == player)
  end,
  on_refresh = function (self, event, target, player, data)
    data.eventData.use.extra_data = data.eventData.use.extra_data or {}
    data.eventData.use.extra_data.lua__mingzhi = true
  end,
})
lua__mingzhi:addEffect(fk.AfterAskForCardResponse, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(lua__mingzhi.name) and data.eventData and data.eventData.use and data.eventData.use.extra_data and
    data.eventData.use.extra_data.lua__mingzhi_round and data.eventData.from ~= player and not data.result
  end,
  on_refresh = function (self, event, target, player, data)
    data.eventData.use.extra_data = data.eventData.use.extra_data or {}
    data.eventData.use.extra_data.lua__mingzhi = true
  end,
})
lua__mingzhi:addEffect(fk.AfterAskForNullification, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(lua__mingzhi.name) and data.eventData and data.eventData.use and data.eventData.use.extra_data and
    data.eventData.use.extra_data.lua__mingzhi_round and data.eventData.from ~= player  and not (data.result and data.result.from == player)
  end,
  on_refresh = function (self, event, target, player, data)
    data.eventData.use.extra_data = data.eventData.use.extra_data or {}
    data.eventData.use.extra_data.lua__mingzhi = true
  end,
})

lua__mingzhi:addEffect(fk.CardUseFinished, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(lua__mingzhi.name, true) and data.extra_data and data.extra_data.lua__mingzhi
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:damage({
      from = nil,
      to = player,
      damage = 1,
      skillName = lua__mingzhi.name,
    })
    player.room:notifySkillInvoked(player, lua__mingzhi.name, "negative")
    player:broadcastSkillInvoke(lua__mingzhi.name, 5)
  end,
})

lua__mingzhi:addEffect(fk.BeforeHpChanged, {
  mute = true,
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(lua__mingzhi.name) and data.skillName ~= lua__mingzhi.name
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.damageEvent and data.damageEvent.card then
      local area = room:getCardArea(data.damageEvent.card)
      if area == Card.Processing or area == Card.DiscardPile or area == Card.DrawPile then
        room:notifySkillInvoked(player, lua__mingzhi.name, "defensive")
        player:broadcastSkillInvoke(lua__mingzhi.name, math.random(1, 4))
        room:delay(2000)
        player:addToPile("lua__mingzhi", data.damageEvent.card, true, lua__mingzhi.name, player)
        local names = Fk:getAllCardNames("btd", true, false)
        local cardsnames = table.map(player:getPile("lua__mingzhi"), function(id) return Fk:getCardById(id).name end)
        if table.every(names, function(c_name) return table.contains(cardsnames, c_name) end) then
          if player.general == "luaqunxian__zhugeliang" or player.deputyGeneral == "luaqunxian__zhugeliang" then
            player.general = "luaqunxian2__zhugeliang"
          end
          room:delay(2000)
          room:gameOver(player.role)
        end
      end
    end
    if not player.dying then
      data.num = 0
    end
    room:notifySkillInvoked(player, lua__mingzhi.name, "defensive")
    if data.reason == "recover" then
      player:broadcastSkillInvoke(lua__mingzhi.name, 6)
    else
      player:broadcastSkillInvoke(lua__mingzhi.name, math.random(1, 4))
    end
    room:delay(2000)
    if data.damageEvent then
      if room:askToSkillInvoke(player, {prompt = "#lua__mingzhi-defensive", skill_name = lua__mingzhi.name }) then
        room:notifySkillInvoked(player, lua__mingzhi.name, "drawcard")
        player:broadcastSkillInvoke(lua__mingzhi.name, math.random(1, 4))
        player:drawCards(2, lua__mingzhi.name)
        if not player:isNude() and not player.dead then
          room:askToDiscard(player, {
            min_num = 1,
            max_num = 999,
            prompt = "#lua__mingzhi-defensive-discard",
            skill_name = lua__mingzhi.name,
            include_equip = true,
            cancelable = true,
          })
        end
      end
    end
  end,
})

lua__mingzhi:addEffect(fk.TurnEnd, {
  anim_type = "masochism",
  audio_index = {1,2,3,4},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(lua__mingzhi.name) and #player:getPile("lua__mingzhi") > 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local can = table.filter(player:getPile("lua__mingzhi"), function (id)
      return not Fk:getCardById(id).is_passive
    end)
    local cards = room:askToChooseCardsAndChoice(player, {
      cards = can,
      all_cards = player:getPile("lua__mingzhi"),
      min_num = 1,
      max_num = #player:getPile("lua__mingzhi"),
      prompt = "#lua__mingzhi-asktocard",
      skill_name = lua__mingzhi.name,
      cancel_choices = {"Cancel"},
      choices = {"OK"},
    })
    if #cards > 0 then
      event:setCostData(self, {card = cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).card
    room:setPlayerMark(player, "@@lua__mingzhi-turn", 1)
    for _, id in ipairs(cards) do
      room:useCard({
        card = Fk:getCardById(id),
        from = player,
        tos = { player },
        extraUse = true,
      })
    end
  end,
})

local spec = {
  anim_type = "control",
  audio_index = {1,2,3,4},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(lua__mingzhi.name) and #player:getPile("lua__mingzhi") > 0 and
    data.responseToEvent and data.responseToEvent.card and table.find(player:getPile("lua__mingzhi"), function (id)
      return Fk:getCardById(id).trueName == data.responseToEvent.card.trueName
    end)
  end,
  on_cost = function (self, event, target, player, data)
    local cards = table.filter(player:getPile("lua__mingzhi"),function (id)
      return Fk:getCardById(id).trueName == data.responseToEvent.card.trueName
    end)
    local card = player.room:askToChooseCardsAndChoice(player, {
      cards = cards,
      all_cards = player:getPile("lua__mingzhi"),
      min_num = 1,
      max_num = 1,
      prompt = "#lua__mingzhi-asktoremove:::"..data.responseToEvent.card:toLogString(),
      skill_name = lua__mingzhi.name,
      cancel_choices = {"Cancel"},
      choices = {"OK"},
    })
    if #card > 0 then
      event:setCostData(self, { card = card })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:moveCardTo(event:getCostData(self).card, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, lua__mingzhi.name, "lua__mingzhi", true, player)
  end,
}

lua__mingzhi:addEffect(fk.CardUseFinished, spec)
lua__mingzhi:addEffect(fk.CardRespondFinished, spec)


return lua__mingzhi
